package com.easy.product.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.Product;
import com.easy.common.service.ProductService;
import com.easy.product.mapper.ProductMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Override
    public Product getProductById(Integer id) {
        return super.getById(id);
    }

    @Override
    public Product getProductByName(String name) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getName, name);
        queryWrapper.eq(Product::getIsdel, 0);
        return super.getOne(queryWrapper);
    }

    @Override
    public Product getProductByStatus(Integer status) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getStatus, status);
        queryWrapper.eq(Product::getIsdel, 0);
        return super.getOne(queryWrapper);
    }

    @Override
    public Product getProductByCategory(String category) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getCategory, category);
        queryWrapper.eq(Product::getIsdel, 0);
        return super.getOne(queryWrapper);
    }

    @Override
    public int addProduct(Product product) {
        return super.save(product)?1:0;
    }

    @Override
    public int updateProduct(Product product) {
        return super.updateById(product)?1:0;
    }

    @Override
    public int deleteProduct(Integer id) {
        return super.removeById(id)?1:0;
    }

    @Override
    public int reverseStatus(Integer id) {
        Product product = super.getById(id);
        if (product != null) {
            product.setStatus(product.getStatus() == 1 ? 0 : 1);
        }
        if (super.updateById(product)) {
            return 1;
        }
        return 0;
    }

    @Override
    public List<Product> getAll(Product product) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Product::getName, product.getName());
        return super.list(queryWrapper);
    }

    @Override
    public IPage<Product> getPage(Product product, IPage page) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Product::getName, product.getName());
        return super.page(page);
    }
}
